package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTable.PrintMode;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import org.joda.time.DateTime;

import domains.Contingent;
import domains.Member;
import managers.Print;
import mappers.ContingentMapper;
import mappers.MemberMapper;

public class ShowMembersGUI {
	private JFrame showMembersFrame;
	private JButton editMemberBtn, closeBtn, removeMemberBtn, printBtn;
	private JTable table;
	private JScrollPane scrollPane;
	private JPanel p;
	private Object id, fName, lName;
	private int index;
	private final MyTableModel myTableModel;
	private JTextField searchTF;
	private JLabel searchLbl;

	public ShowMembersGUI() {
		MemberMapper mMapper = new MemberMapper();
		List<Member> data = mMapper.selectAllMembers();
		myTableModel = new MyTableModel(data);

		table = new JTable(myTableModel) {

			private static final long serialVersionUID = 1L;

			public Component prepareRenderer(TableCellRenderer renderer,
					int row, int column) {
				Component c = super.prepareRenderer(renderer, row, column);
				return c;
			}
		};

		final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
				myTableModel);

		scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowSorter(sorter);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					table = (JTable) e.getSource();
					int row = table.getSelectedRow();
					Object id = (Object) table.getValueAt(row, 0);
					index = (Integer) id;
			
					ContingentHistoryGUI cHistoryGUI = new ContingentHistoryGUI(ShowMembersGUI.this);
					cHistoryGUI.showGUI();
				}
			}
		});
		
		DefaultTableCellRenderer leftRenderer = new DefaultTableCellRenderer();
		leftRenderer.setHorizontalAlignment(JLabel.LEFT);
		table.getColumnModel().getColumn(0).setCellRenderer(leftRenderer);
		
		table.getColumnModel().getColumn(0).setPreferredWidth(90);
		table.getColumnModel().getColumn(1).setPreferredWidth(115);
		table.getColumnModel().getColumn(2).setPreferredWidth(105);
		table.getColumnModel().getColumn(3).setPreferredWidth(170);
		table.getColumnModel().getColumn(4).setPreferredWidth(110);
		table.getColumnModel().getColumn(5).setPreferredWidth(120);
		table.getColumnModel().getColumn(6).setPreferredWidth(200);
		table.getColumnModel().getColumn(7).setMinWidth(150);
		
		searchLbl = new JLabel("Søg");
		searchTF = new JTextField(10);

		searchTF.getDocument().addDocumentListener(new DocumentListener() {

			private void searchFieldChangedUpdate(DocumentEvent evt) {
				String text = searchTF.getText();
				if (text.isEmpty()) {
					sorter.setRowFilter(null);
				} else {
					try {
						sorter.setRowFilter(RowFilter.regexFilter(
								"(?i)" + text, 0, 1, 2));

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public void insertUpdate(DocumentEvent evt) {
				searchFieldChangedUpdate(evt);
			}

			@Override
			public void removeUpdate(DocumentEvent evt) {
				searchFieldChangedUpdate(evt);
			}

			@Override
			public void changedUpdate(DocumentEvent evt) {
				searchFieldChangedUpdate(evt);
			}
		});

		editMemberBtn = new JButton("Rediger Medlem");
		editMemberBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				EditMemberGUI editMemberGUI = new EditMemberGUI(
						ShowMembersGUI.this);
				int row = table.getSelectedRow();
				// int column = table.getSelectedColumn();
				id = (Object) table.getValueAt(row, 0);
				Object fName = (Object) table.getValueAt(row, 1);
				Object lName = (Object) table.getValueAt(row, 2);
				Object address = (Object) table.getValueAt(row, 3);
				Object zipCode = (Object) table.getValueAt(row, 4);
				Object city = (Object) table.getValueAt(row, 5);
				Object email = (Object) table.getValueAt(row, 6);
				Object phoneNo = (Object) table.getValueAt(row, 7);

				index = (Integer) id;
				editMemberGUI.getfNameTF().setText(fName.toString());
				editMemberGUI.getlNameTF().setText(lName.toString());
				editMemberGUI.getAddressTF().setText(address.toString());
				editMemberGUI.getZipCodeTF().setText(zipCode.toString());
				editMemberGUI.getCityTF().setText(city.toString());
				editMemberGUI.getEmailTF().setText(email.toString());
				editMemberGUI.getPhoneNoTF().setText(phoneNo.toString());
				editMemberGUI.showGUI();

			}
		});

		removeMemberBtn = new JButton("Fjern Medlem");
		removeMemberBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				// int column = table.getSelectedColumn();
				id = (Object) table.getValueAt(row, 0);
				fName = (Object) table.getValueAt(row, 1);
				lName = (Object) table.getValueAt(row, 2);

				Object[] options = { "Ja", "Nej" };
				int n = JOptionPane
						.showOptionDialog(null,
								"Er du sikker du vil slette medlem?\n\n"
										+ "Medlemsnr: " + id + "\n\n" + fName
										+ " " + lName, "Advarsel",
								JOptionPane.YES_NO_OPTION,
								JOptionPane.QUESTION_MESSAGE, null, options,
								options[0]);

				if (n == JOptionPane.YES_OPTION) {
					MemberMapper mMapper = new MemberMapper();
					int i = (Integer) id;
					mMapper.removeMember(i);
					myTableModel.removeMemberFromTable(row);
					myTableModel.fireTableDataChanged();
				} else {
				}

			}
		});

		closeBtn = new JButton("Close");
		closeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				showMembersFrame.setVisible(false);
			}
		});

		printBtn = new JButton("Print");
		printBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new Print(table);
			}
		});

		p = new JPanel(new FlowLayout());

		JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));

		p1.add(searchLbl);
		p1.add(searchTF);
		p2.add(editMemberBtn);
		p2.add(removeMemberBtn);
		p2.add(closeBtn);
		p2.add(printBtn);

		p.add(p1);
		p.add(p2);
	}

	public void showGUI() {
		showMembersFrame = new JFrame();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
		int w = showMembersFrame.getSize().width;
		int h = showMembersFrame.getSize().height;
		int x = (dim.width-w)/8;
		int y = (dim.height-h)/8;
		showMembersFrame.setLocation(x, y);
		showMembersFrame.setTitle("Vis Medlemmer");
		showMembersFrame.setLayout(new BorderLayout());
		showMembersFrame.setSize(1000, 600);
		showMembersFrame.add(scrollPane, BorderLayout.CENTER);
		showMembersFrame.add(p, BorderLayout.SOUTH);
		showMembersFrame.setVisible(true);
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

}

class MyTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private List<Member> members = new ArrayList<Member>();

	public MyTableModel(List<Member> members) {
		this.members = members;
	}

	public void addMember(Member m) {
		members.add(m);
		int row = members.size() - 1;
		fireTableRowsInserted(row, row);
	}

	public void removeMemberFromTable(int row) {
		members.remove(row);
		fireTableRowsDeleted(row, row);
	}

	public void tableUpdate(int row, Member m) {
		if (row >= 0 && row < members.size()) {
			members.set(row, m);
			fireTableRowsUpdated(row, row);
		}
	}

	public List<Member> getMembers() {
		return new ArrayList<Member>(members);
	}

	@Override
	public int getColumnCount() {
		return 8;
	}

	@Override
	public int getRowCount() {
		return members.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Member m = members.get(rowIndex);

		switch (columnIndex) {
		case 0:
			return m.getMemberId();

		case 1:
			return m.getfName();

		case 2:
			return m.getlName();

		case 3:
			return m.getAddress();

		case 4:
			return m.getZipCode();

		case 5:
			return m.getCity();

		case 6:
			return m.getEmail();

		case 7:
			return m.getPhoneNo();

		default:
			return "";
		}
	}

	public String getColumnName(int column) {
		switch (column) {
		case 0:
			return "MedlemsID";

		case 1:
			return "Fornavn";

		case 2:
			return "Efternavn";

		case 3:
			return "Adresse";

		case 4:
			return "Postnummer";

		case 5:
			return "By";

		case 6:
			return "Email";

		case 7:
			return "Telefonnr";

		default:
			return "";
		}
	}

	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {
		case 0:
			return Integer.class;

		case 1:
			return String.class;

		case 2:
			return String.class;

		case 3:
			return String.class;

		case 4:
			return String.class;

		case 5:
			return String.class;

		case 6:
			return String.class;

		case 7:
			return String.class;

		default:
			return String.class;
		}
	}
}
